package com.abocode.aichat.configuration;

import com.abocode.aichat.core.application.AuthHandler;
import com.abocode.aichat.core.application.ChatMessageHandler;
import com.abocode.aichat.server.context.ServerRuntimeContext;
import com.abocode.aichat.server.endpoint.ProtocolEndpoint;
import com.abocode.aichat.server.modules.Module;
import com.abocode.aichat.server.modules.ModuleHandler;
import com.abocode.aichat.server.modules.protocol.AuthModule;
import com.abocode.aichat.server.modules.protocol.ChatModule;
import com.abocode.aichat.server.modules.protocol.PresenceModule;
import com.abocode.aichat.server.modules.protocol.handler.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class AppConfiguration {
    @Value("${im.domain}")
    private  String domain;
    @Value("${im.port}")
    private  int port;
    @Autowired
    private ServerRuntimeContext serverRuntimeContext;

    @Bean(initMethod = "start",destroyMethod = "shutdown")
    public ProtocolEndpoint protocolEndpoint(){
        ProtocolEndpoint protocolEndpoint=new ProtocolEndpoint();
        protocolEndpoint.setName("protocol");
        protocolEndpoint.setModules(modules());
        protocolEndpoint.setDomain(domain);
        protocolEndpoint.setPort(port);
        protocolEndpoint.setChildSize(100);
        protocolEndpoint.setParentSize(100);
        protocolEndpoint.setServerRuntimeContext(serverRuntimeContext);
        return protocolEndpoint;
    }
    @Bean
    public List<Module> modules(){
        List<Module> modules=new ArrayList();
        modules.add(authModule());
        modules.add(chatModule());
        modules.add(presenceModule());
        return modules;
    }

    @Autowired
    public AuthHandler authHandler;
    @Bean
    public AuthModule authModule(){
        List<ModuleHandler> handlers=new ArrayList();
        handlers.add(authHandler);
        AuthModule authModule=new AuthModule();
        authModule.setModuleHandlers(handlers);
        return  authModule;
    }

    @Autowired

    private  ChatMessageHandler chatMessageHandler;

    @Bean
    public ChatModule chatModule(){
        List<ModuleHandler> handlers=new ArrayList();
        handlers.add(chatConfirmHandler());
        handlers.add(chatOfflineHandler());
        handlers.add(chatMessageHandler);

        ChatModule chatModule=new ChatModule();
        chatModule.setModuleHandlers(handlers);
        return  chatModule;
    }
    @Bean
    ChatConfirmHandler chatConfirmHandler(){
        return  new ChatConfirmHandler();
    }
    @Bean
    ChatOfflineHandler chatOfflineHandler(){
        return  new ChatOfflineHandler();
    }

    @Bean
    public PresenceModule presenceModule(){
        List<ModuleHandler> handlers=new ArrayList();
        handlers.add(presenceGetHandler());
        handlers.add(presenceOnlineHandler());
        handlers.add(presenceSetHandler());
        PresenceModule presenceModule=new PresenceModule();
        presenceModule.setModuleHandlers(handlers);
        return  presenceModule;
    }
    @Bean
    public PresenceGetHandler presenceGetHandler(){
         return  new PresenceGetHandler();
    } @Bean
    public PresenceOnlineHandler presenceOnlineHandler(){
         return  new PresenceOnlineHandler();
    } @Bean
    public PresenceSetHandler presenceSetHandler(){
         return  new PresenceSetHandler();
    }

}
